package com.situ.blog.service.impl;


import com.situ.blog.dao.IBlogDao;
import com.situ.blog.dao.impl.BlogDaoImpl;
import com.situ.blog.listener.BlogExcelListener;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.util.ExcelUtil;
import com.situ.blog.util.PageResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class BlogServiceImpl implements IBlogService {
    private IBlogDao blogDao = new BlogDaoImpl();

    @Override
    public PageResult selectByPage(BlogQuery blogQuery) {
        List<Blog> list = blogDao.selectByPage(blogQuery);
        int totalCount = blogDao.selectTotalCount(blogQuery);

        PageResult<Blog> pageResult = new PageResult<>(0, "", totalCount, list);
        return pageResult;
    }

    @Override
    public void deleteById(int id) {
        blogDao.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        //delete from blog where id in(2,3)
        for (String id : ids) {
            blogDao.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Blog blog) {
        blogDao.add(blog);
    }

    @Override
    public Blog selectById(int id) {
        return blogDao.selectById(id);
    }

    @Override
    public void update(Blog blog) {
        blogDao.update(blog);
    }

    @Override
    public void updateStatus(String id, int status) {
        blogDao.updateStatus(id, status);
    }

    @Override
    public void exportExcel(HttpServletResponse resp) {
        List<Blog> list = blogDao.selectAll();
        ExcelUtil.exportExcel(resp, list, Blog.class, "blog");
    }

    @Override
    public void importExcel(HttpServletRequest req) {
        ExcelUtil.importExcel(req, new BlogExcelListener(), Blog.class);
    }
}
